package com.siyoumi.app.modules.app_book.task;

import com.siyoumi.app.entity.BookOrder;
import com.siyoumi.app.modules.app_book.service.SvcBookOrder;
import com.siyoumi.component.LogPipeLine;
import com.siyoumi.component.XApp;
import com.siyoumi.component.http.XHttpContext;
import com.siyoumi.config.SysConfig;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.task.EnumTask;
import com.siyoumi.task.TaskController;
import com.siyoumi.util.XDate;
import com.siyoumi.util.XReturn;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//主播分账，n+2
//http://dev.x.siyoumi.com/task/app_book/task_order_share_sales
@RestController
@RequestMapping("/task/app_book/task_order_share_sales")
public class TaskOrderShareSales
        extends TaskController {

    @Override
    protected Boolean handle1() {
        return false;
    }

    @Override
    protected List handleData() {
        //06-07 入住
        //06-09 分账

        Integer todayNum = XDate.toNum(XDate.today());

        JoinWrapperPlus<BookOrder> query = SvcBookOrder.getApp().join();
        query.in("border_status", 15, 20) //入住中开始处理
                .eq("border_share_sales_handle", 0) //每天处理1次
                .between("border_share_sales_handle_index", 0, todayNum)
                .lt("border_share_sales_handle_date", XDate.now());
        ;
        if (!SysConfig.getBean().isDev()) {
            query.le("border_date_begin", XDate.today().minusDays(2)); //入住n+2
        }
        query.orderByAsc("border_share_sales_handle_date");

        return SvcBookOrder.getApp().get(query);
    }

    @Override
    protected void handleBefore(Object data) {
        BookOrder entity = (BookOrder) data;

        BookOrder entityOrderUpdate = new BookOrder();
        entityOrderUpdate.setBorder_id(entity.getKey());
        entityOrderUpdate.setBorder_share_sales_handle_date(XDate.now().plusMinutes(3));
        SvcBookOrder.getApp().updateById(entityOrderUpdate);
    }

    @Override
    protected XReturn handle(Object data) {
        BookOrder entity = (BookOrder) data;
        XHttpContext.setX(entity.getBorder_x_id());

        LogPipeLine.ofSys().setLogMsg("开始处理主播分账：", entity.getKey());
        LogPipeLine.ofSys().setAppendData("log_str_00", entity.getKey());

        XApp.getTransaction().execute(status -> {
            XReturn r = SvcBookOrder.getBean().shareSalesPrice(entity);

            LogPipeLine.ofSys().setLogMsg(r.toString());
            //更新分帐状态
            BookOrder entityOrderUpdate = new BookOrder();
            entityOrderUpdate.setBorder_id(entity.getKey());
            entityOrderUpdate.setBorder_share_sales_handle(r.getErrCode());
            if (r.ok()) {
                entityOrderUpdate.setBorder_share_sales_handle(1);
            }
            if (r.getErrCode() == 5) { //明天继续执行
                entityOrderUpdate.setBorder_share_sales_handle(0);
                entityOrderUpdate.setBorder_share_sales_handle_index(XDate.toNum(XDate.today().plusDays(1)));
            }
            entityOrderUpdate.setBorder_share_sales_handle_date(XDate.now());
            SvcBookOrder.getApp().updateById(entityOrderUpdate);

            return r;
        });

        return EnumTask.RUN.getR();
    }
}
